/*
 * Copyright (C) 2000-2007 Wang Pengcheng <wpc0000@gmail.com>
 * Licensed to the Wang Pengcheng under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The LGPL licenses this file to You under the GNU Lesser General Public
 * Licence, Version 2.0  (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/lgpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//28 Aug 2007
package cn.edu.whu.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import cn.edu.whu.data.*;

/**
 * The iBATISTemplate's control.
 * It is a command line system.
 * @author wpc
 * @version 0.0.1
 */
public class Control {

	/**
	 * The main system.
	 * @param args
	 * @throws Exception iostream
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//Set the control,but I make the mistake,in this method
		//It will set all the vars.
		Control c=new Control();
		//Begin Create all files
		c.run();
	}

	// Save the data which will be inserted the database
	private DataList list = new DataList();

	// Save the template vars
	private VarList var = new VarList();

	// Save the commands that asks add controls
	private List<String> addList = new ArrayList<String>();

	// Edit the DatabaseBeanAction.java
	private EditAction ea = new EditAction(var, list);

	// Get the basic information about this template
	private GetInfor gi = new GetInfor(list, var);

	// Set the inputstream
	private InputStream s = System.in;

	// Edit the DatabaseBean.java
	private EditBean eb = new EditBean(var, list);

	// Edit the SQLMapConfig.xml
	private EditConfigXml ecx = new EditConfigXml(var, list);
	
	// Edit the DataBase.xml
	private EditBeanXml ebx = new EditBeanXml(var, list);

	private EditDBSQL edb = new EditDBSQL(var,list);
	
	//
	private EditConfigPer ecp=   new EditConfigPer(var);

	// The control's input stream
	private Scanner in = new Scanner(System.in);

	/**
	 * The default constructor of this system.
	 * It will clear the addList which is using for saving the input data.
	 * And make the inputstream for the control and getBasicSystem.
	 * Then get the basic information for the database.
	 * At last make the the dictionaries for the database and set the path for the edit classes.
	 * @throws Exception the exception about the io stream
	 */
	public Control() throws Exception {
		//Make a addList and clear it
		addList.clear();
		//
		makeInputputStream();
		getInformation();
		
		mkdir();
		setPath();
	}
	
	/**
	 * Edit the javaAction.java and the javaBean.xml
	 * @throws Exception  the exception about the io stream
	 */
	public void editActionAndBeanXml() throws Exception {
		ea.read();
		ebx.read();
		getList();
		ea.write();
		ebx.write();
	}
	
	/**
	 * Edit the SQLMapConfig.properties
	 * @throws Exception  the exception about the io stream
	 */
	public void editConfigPer() throws Exception{
		
		//Read the template files
		ecp.read();
		
		//Write the aim files
		ecp.write();
	}
	
	/**
	 * Edit the javaBean.java
	 * @throws Exception  the exception about the io stream
	 */
	public void editDatabaseBean() throws Exception {
		eb.read();
		eb.add();
		eb.write();
	}

	/**
	 * Edit the create_.sql
	 * @throws Exception
	 */
	public void editDataBaseCreateFile() throws Exception{
		edb.read();
		edb.change();
		edb.write();
	}
	/**
	 * Edit the SQLMapConfig.xml
	 * @throws Exception  the exception about the io stream
	 */
	public void editSQLMapConfig() throws Exception {
		ecx.read();
		ecx.change();
		ecx.write();
	}
	
	/**
	 * Get the basic information for the system.
	 *
	 */
	public void getInformation() {
		gi.get();
	}

	/**
	 * Get the sql sentences.
	 *
	 */
	public void getList() {
		String s = "";
		do {
			System.out.print("Please enter the action(\"quit\" to exit):");
			s = in.nextLine();
			if (s.contains("add")) {
				String addString = s.substring(s.indexOf(" ") + 1);
				ea.add(addString);
				ebx.add(addString);
			}
		} while (!s.contains("quit"));
	}

	/**
	 * Make the input stream for the getInformationsystem and the control system
	 * but it be setted in the editting.
	 * And the default input systems are the system.in .
	 * @throws Exception the exception about the io stream
	 */
	public void makeInputputStream() throws Exception{
		
		//Set the getInformation system 's input stream.
		s = new FileInputStream("javaBean.txt");
		
		//Set the control system's input stream
		in = new Scanner(new FileInputStream("dbAction.txt"));
		
		//Set the inputstream to the getInformationsystem
		gi.setInput(s);
	}

	/**
	 * Make the database package 's dictionaries.
	 *
	 */
	public void mkdir() {

		System.out.println("\n");

		String s = var.getValue("$PKGURL");

		String[] stemp = s.split("/");

		String[] spath = new String[stemp.length];

		spath[0] = stemp[0];
		for (int i = 1; i < spath.length; i++) {
			spath[i] = spath[i - 1] + BasicDataForEdit.DIR_SPLIT + stemp[i];
		}

		String filePath = "";
		java.io.File myFilePath = null;
		filePath = spath[spath.length-1];
		myFilePath =new File(filePath);
		myFilePath.mkdirs();
//		for (int i = 0; i < spath.length; i++) {
//			filePath=spath[i];
//			filePath = filePath.toString();
//			myFilePath = new java.io.File(filePath);
//			if (!myFilePath.exists()) {
//				boolean b = myFilePath.mkdir();
//				System.out.println("Creat " + filePath + " " + b);
//			}
//		}

		String[] sp = { s + BasicDataForEdit.DIR_SPLIT +"database", s +BasicDataForEdit.DIR_SPLIT + "/databaseAction","src" };
		for(String t:sp){
			myFilePath = new File(t);
			myFilePath.mkdirs();
		}
//		for (int i = 0; i < sp.length; i++) {
//			filePath = sp[i];
//			filePath = filePath.toString();
//			myFilePath = new java.io.File(filePath);
//			if (!myFilePath.exists()) {
//				boolean b = myFilePath.mkdir();
//				System.out.println("Creat " + filePath + " " + b);
//			}
//		}
	}

	/**
	 * Run the edit system and create the template files.
	 * @throws Exception  the exception about the io stream
	 */
	public void run() throws Exception{
		
		//Edit the SQLMapConfig.properties
		editConfigPer();
		
		//Edit the javaBean for the database
		editDatabaseBean();
		
		//Edit the SqlMapConfig.xml
		editSQLMapConfig();
		
		//Edit the javaAction.java and the javaBean.xml for the sql sentences.
		editActionAndBeanXml();
		
		//Edit the sql file
		editDataBaseCreateFile();
	}

	/**
	 * Set the system path for the edit classes to create the right files and
	 * location.
	 * 
	 */
	public void setPath(){
		String p=var.getValue("$PKGURL");
		String name=var.getValue("$DATA");
		//p = p.replaceAll("/", BasicDataForEdit.DIR_SPLIT);
		p=p.replace("/",BasicDataForEdit.DIR_SPLIT);
		
		ea.setPath(p+BasicDataForEdit.DIR_SPLIT +"databaseAction"+BasicDataForEdit.DIR_SPLIT +name+"Action.java");
		
		eb.setPath(p+BasicDataForEdit.DIR_SPLIT +"database"+BasicDataForEdit.DIR_SPLIT +name+"Bean.java");
		
		ecx.setPath(p+BasicDataForEdit.DIR_SPLIT +"databaseAction"+BasicDataForEdit.DIR_SPLIT +"SQLMapConfig.xml");
		
		ebx.setPath(p+BasicDataForEdit.DIR_SPLIT +"databaseAction"+BasicDataForEdit.DIR_SPLIT +name+".xml");
		
		ecp.setPath(p+BasicDataForEdit.DIR_SPLIT +"databaseAction"+BasicDataForEdit.DIR_SPLIT +"SqlMapConfig.properties");
		
		edb.setPath("src"+BasicDataForEdit.DIR_SPLIT +"create_"+name+".sql");
	}

}
